package com.study.util.db;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2022/7/5 2:40 下午
 */
public class DbFormatPrintUtil {



    public static String printDbField(String fieldStr) {
        if(StringUtils.isEmpty(fieldStr)) {
            return null;
        }
        String line = fieldStr.trim().replace("`", " ").replace("COMMENT", " ").replace("'", " ").replace(",", " ");
        line = line.replace(" NOT NULL "," ").replace(" DEFAULT "," ").replace(" NULL "," ")
                .replace(" AUTO_INCREMENT "," ").replace(" timestamp "," ")
                .replace(" ON "," ").replace(" UPDATE "," ").replace(" CURRENT_TIMESTAMP "," ");
        String[] strs = line.split(" ");
        List<String> list = new ArrayList<>();
        for (String str:strs) {
            if(StringUtils.isEmpty(str)) {
                continue;
            }
            list.add(str);
        }
        return StringUtils.join(list,"   ");
    }


    public static void main(String[] args) {
        String str = "`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',\n" +
                "  `type` int(2) NOT NULL COMMENT '奖励类型(1、减额,2、折扣,3、包邮,4、优惠券,5、虚拟币,6、返现,7、最后一件折扣)',\n" +
                "  `template_id` bigint(20) NOT NULL COMMENT '模板id',\n" +
                "  `value` varchar(1024) NOT NULL COMMENT '属性值(比如金额,或其它json)',\n" +
                "  `is_delete` int(2) NOT NULL COMMENT '删除状态',\n" +
                "  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
                "  `modify_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',";
        String[] split = str.split("\n");
        for (String s:split) {
            System.out.println(printDbField(s));
            System.out.println();
        }

        List<Integer> collect = Lists.newArrayList(18, 8, 2, 20, 2, 4, 8).stream().sorted().collect(Collectors.toList());
        System.out.println(collect);
    }

}
